package appUtils;

import android.app.Activity;
import android.os.Process;

import java.util.ArrayList;
import java.util.List;

import apiUtils.BaseAPIS;

/**
 * Created by sth0409 on 2017/03/28-14:17.
 */

public class AppUtil {
    private List<Activity> activityList = new ArrayList<>();

    private AppUtil() {

    }

    public synchronized static AppUtil getInstance() {
        return holder.appUtil;
    }

    public void pushAC(Activity activity) {
        activityList.add(activity);
    }

    public void removeAC(Activity activity) {
        activityList.remove(activity);
    }

    public List<Activity> getActivityList() {
        return activityList;
    }

    public void finishAc(Class<?> clazz) {
        for (Activity ac : activityList) {
            if (ac.getClass().equals(clazz)) {
                ac.finish();
            }
        }
    }

    private void finishAllAC() {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    private void finishAllAC(Activity ac) {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    public void exit() {
        BaseAPIS.getInstance().destroy();
        finishAllAC();
        Process.killProcess(Process.myPid());
    }

    private static class holder {
        private static final AppUtil appUtil = new AppUtil();
    }
}
